package cn.kgc.service.impl;

import cn.kgc.entity.GameNum;
import cn.kgc.entity.Order;
import cn.kgc.entity.Shop;
import cn.kgc.entity.UserB;
import cn.kgc.mapper.UserMapper;
import cn.kgc.service.userService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class userServiceImpl implements userService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean uploadHead(Integer id, String pic) {
        if (userMapper.uploadHead(id, pic) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int getGold(Integer id) {
        return userMapper.getGold(id);
    }

    @Transactional
    @Override
    public boolean userAdd(UserB user) {
        if (userMapper.userAdd(user) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public UserB login(String userName, String pwd) {
        return userMapper.login(userName, pwd);
    }

    @Override
    public List<Shop> shopList(Integer currentPage, Integer pageSize, String name1) {
        return userMapper.shopList(currentPage, pageSize, name1);
    }

    @Override
    public int totalCount(String name1) {
        return userMapper.totalCount(name1);
    }

    @Override
    public List<GameNum> gnList() {
        return userMapper.gnList();
    }

    @Override
    public boolean addShop(Shop s) {
        if (userMapper.addShop(s) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Shop getShop(int id) {
        return userMapper.getShop(id);
    }

    @Override
    public UserB getUser(String userName, String pwd) {
        return userMapper.getUser(userName, pwd);
    }

    @Override
    public UserB getUserByName(String userName) {
        return userMapper.getUserByName(userName);
    }

    @Override
    public boolean checkUName(String uName) {
        if (userMapper.checkUName(uName) <= 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkUserName(String userName) {
        if (userMapper.checkUserName(userName) <= 0) {
            return true;
        }
        return false;
    }

    @Override
    public UserB getUserg(String uName) {
        return userMapper.getUserg(uName);
    }

    @Override
    public boolean cMoney(String uName, double gold) {
        if (userMapper.cMoney(uName, gold) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Shop updateShopStatus(int id, int status, String uName) {
        int a = userMapper.updateShopStatus(id, status);
        if (a > 0) {
            UserB userB = userMapper.getUserByUName(uName);
            int zNum = userB.getzNum() + 1;
            int b = userMapper.updateNumByName(zNum, uName);
            return userMapper.getNumPwdById(id);
        }
        return null;
    }

    @Override
    public UserB getShopList(int currentPage, int pageSize, int id) {
        List<Shop> shops = userMapper.getShopList2(currentPage, pageSize, id);
        UserB userB = userMapper.getShopList(id);
        userB.setShopList(shops);
        return userB;
    }

    @Override
    public int getShopCount(int id) {
        return userMapper.getShopCount(id);
    }

    @Override
    public boolean checkShop(int id) {
        if (userMapper.checkShop(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteShop(int id) {
        if (userMapper.deleteShop(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateShop(Shop shop) {
        if (userMapper.updateShop(shop) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Shop> moHuList(int currentPage, int pageSize, String title) {
        return userMapper.moHuList(currentPage, pageSize, title);
    }

    @Override
    public int moHuTotalCount(String title) {
        return userMapper.moHuTotalCount(title);
    }


    @Transactional
    @Override
    public int scShop(int userId, int shopId) {

        try {
            int flag = userMapper.scCheck(userId, shopId);
            if (flag == 0) {
                if (userMapper.scShop2(userId, shopId) < 0) {
                    return 0;
                }
                return userMapper.scShop(userId, shopId);
            } else {
                return -10;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Shop> getSCShopList(Integer userId) {
        return userMapper.getSCShopList(userId);
    }

    @Override
    public int getTotalSCShopCount(int userId) {
        return 0;
    }

    @Transactional
    @Override
    public boolean deleteSCShop(int userId, int shopId) {
        boolean flag = false;
        if (userMapper.delete2SCShop(shopId) > 0) {
            flag = true;
        } else {
            flag = false;
        }
        if (userMapper.deleteSCShop(userId, shopId) > 0) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    @Override
    public List<Order> getSCOrderList(int userId) {
        return userMapper.getSCOrdersList(userId);
    }

    @Override
    public double findShopPrice(int id) {
        return userMapper.findShopPrice(id);
    }

    @Override
    public void addUserOrder(Integer uid, int pid, Integer hour, double zmoney) {
        userMapper.addUserOrder(uid, pid, hour, zmoney);
    }

    @Override
    public boolean deleteOrders(Integer userId, Integer shopId) {
        if (userMapper.deleteOrders(userId,shopId) != 0) {
            return true;
        }
        return false;
    }

    @Override
    public int upDateMoney(Double zgoldn, int id) {
        return userMapper.upDateMoney(zgoldn, id);
    }
}
